package com.jn.erp.wechat.mp.handler;

import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * <pre>
 *  小程序信息相关
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/25 14:05
 */
//@AllArgsConstructor
@Component
public class MpInfoHandler {

    private final WxMpService wxMpService;

    @Autowired
    public MpInfoHandler(@Qualifier("wxMpJnService") WxMpService wxMpService){
        this.wxMpService = wxMpService;
    }

    //登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
    public WxOAuth2AccessToken oAuth2AccessToken(String code) {
        try {
            WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
            return accessToken;
        } catch (WxErrorException e) {
            System.out.println(e.getError());
            return null;
        }

    }


    public String  qrcodeCreate(String param)   {
        String res="";
        try {
            WxMpQrCodeTicket wxMpQrCodeTicket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket(param, 604800);
            System.out.println(wxMpQrCodeTicket);
            File file = wxMpService.getQrcodeService().qrCodePicture(wxMpQrCodeTicket);
            System.out.println(file);
            res=wxMpService.getQrcodeService().qrCodePictureUrl(wxMpQrCodeTicket.getTicket());
        }catch (WxErrorException e){
            System.out.println(e.getMessage());
        }
        return res;
    }

//
//    //登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
//    public WxOAuth2AccessToken oAuth2AccessToken2(String code) {
//        String url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
//
//
//        try {
//            Map<String, String> params = ImmutableMap.of("code", code);
//            Map<String, String> params = ImmutableMap.of("appid", code);
//            Map<String, String> params = ImmutableMap.of("secret", code);
//            Map<String, String> params = ImmutableMap.of("code", code);
//            String post = wxMpService.post("https://api.weixin.qq.com/sns/oauth2/access_token", WxMaGsonBuilder.create().toJson(params));
//            JSONObject info = JSONObject.parseObject(post);
//            WxMaConfigHolder.remove();//清理ThreadLocal
//            return info;
//        } catch (WxErrorException e) {
//            System.out.println(e.getError());
//            return null;
//        }
//
//        try {
//            WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
//            return accessToken;
//        } catch (WxErrorException e) {
//            System.out.println(e.getError());
//            return null;
//        }
//
//    }


}
